﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FimetContoDeposito.BLL.Objects;
using FimetContoDeposito.DAL;
using GrayParrot.Core;

namespace FimetContoDeposito.BLL.Facades
{
    public class MagazziniFacade : FacadeBase<MagazziniFacade>
    {
        FCDContext m_ctx = null;

        public Repository<FCD_Magazzini, Guid> Repo { get; internal set; }


        public MagazziniFacade()
        {
        }

        public override void Initialize()
        {
            m_ctx = new FCDContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;

            Repo = this.GetRepository<FCD_Magazzini, Guid>(m_ctx);
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();

                Repo.Dispose();
            }
        }

        public ReturnValue<bool> Add(FCD_Magazzini item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                if (item.Id == Guid.Empty)
                {
                    item.Id = Guid.NewGuid();
                }

                this.Repo.Add(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(FCD_Magazzini item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Edit(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(FCD_Magazzini item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Delete(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<FCD_Magazzini> Get(Guid id)
        {
            ReturnValue<FCD_Magazzini> retValue = new ReturnValue<FCD_Magazzini>();

            try
            {
                retValue.Value = this.Repo.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FCD_Magazzini>> GetAll()
        {
            ReturnValue<IQueryable<FCD_Magazzini>> retValue = new ReturnValue<IQueryable<FCD_Magazzini>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FCD_Magazzini>> GetAllByCustomer(Guid idCustomer)
        {
            ReturnValue<IQueryable<FCD_Magazzini>> retValue = new ReturnValue<IQueryable<FCD_Magazzini>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll().Where(x=>x.IdCustomer == idCustomer);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        //public ReturnValue<IQueryable<FCD_Magazzini>> GetAllByArticolo(Guid idArticolo)
        //{
        //    ReturnValue<IQueryable<FCD_Magazzini>> retValue = new ReturnValue<IQueryable<FCD_Magazzini>>();
        //    retValue.HasValue = false;
        //    retValue.Value = null;

        //    try
        //    {
        //        var objs = from x in m_ctx.FCD_Movimentis
        //                   where x.Articolo == idArticolo
        //                   select x;

        //        if (objs.Any())
        //        {
        //            retValue.Value = objs;
        //            retValue.HasValue = true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        retValue.HasError = true;
        //        retValue.Error = ex;
        //    }

        //    return retValue;
        //}

        public ReturnValue<IQueryable<FCD_Magazzini>> GetAllEx()
        {
            ReturnValue<IQueryable<FCD_Magazzini>> retValue = new ReturnValue<IQueryable<FCD_Magazzini>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll(null, "FCD_Customer");
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        //public ReturnValue<FCD_Magazzino> GetCustomerData(Guid idCustomer, Guid idArticolo)
        //{
        //    ReturnValue<FCD_Magazzino> retValue = new ReturnValue<FCD_Magazzino>();
        //    retValue.Value = null;
        //    retValue.HasValue = true;

        //    try
        //    {
        //        var objs = from x in m_ctx.FCD_Magazzinoes
        //                   where x.IdCustomer == idCustomer && x.Articolo == idArticolo
        //                   select x;

        //        if (objs.Any())
        //        {
        //            retValue.Value = objs.FirstOrDefault();
        //            retValue.HasValue = true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        retValue.HasError = true;
        //        retValue.Error = ex;
        //    }

        //    return retValue;
        //}

        //public ReturnValue<IQueryable<SituazioneMagazzino>> GetSituazioneMagazzino()
        //{
        //    ReturnValue<IQueryable<SituazioneMagazzino>> retValue = new ReturnValue<IQueryable<SituazioneMagazzino>>();
        //    retValue.HasValue = false;
        //    retValue.Value = null;

        //    try
        //    {
        //        var objs = from x in m_ctx.FCD_Magazzinoes
        //                   select new SituazioneMagazzino()
        //                   {
        //                       Id = x.Id,
        //                       Cliente = x.FCD_Customer.RagioneSociale,
        //                       Prenotata = x.Prenotata.Value,
        //                       Articolo = x.FCD_AnagraficaArticoli.Descrizione,
        //                       Giacenza = x.Giacenza
        //                   };

        //        if (objs.Any())
        //        {
        //            retValue.Value = objs;
        //            retValue.HasValue = true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        retValue.HasError = true;
        //        retValue.Error = ex;
        //    }

        //    return retValue;
        //}


    }
}
